package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Logininfo;
import cn.itsource.org.domain.User;
import cn.itsource.org.domain.dto.UserSmsCodeDto;
import cn.itsource.org.mapper.LogininfoMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.org.service.IUserService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhm
 * @since 2023-03-27
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public AjaxResult userRegister(UserSmsCodeDto dto) {
        //参数校验
        //验证码校验
        //拼接短信验证码redis存储的key
        String redisKey = dto.getMobile()+":"+"register";
        //去redis中查询
        Object o = redisTemplate.opsForValue().get(redisKey);
        if(o == null){
            throw new CustomException("短信验证码不正确");
        }
        //取验证码的值
        String code = o.toString().split(":")[0];
        //校验短信验证码是否填写正确
        if(!code.equals(dto.getSmsCode())){
            throw new CustomException("短信验证码不正确");
        }
        //密码校验
        if(StringUtils.isEmpty(dto.getPassword()) || StringUtils.isEmpty(dto.getConfirmPassword())){
            throw new CustomException("请重新输入密码");
        }
        //判断密码是否一致
        if(!dto.getPassword().equalsIgnoreCase(dto.getConfirmPassword())){
            throw new CustomException("两次输入的密码不一致");
        }
        //加密
        //生成盐值
        String salt = StrUtils.getComplexRandomString(128);
        //加密后的密码
        String encrypPwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        //保存logininfo
        Logininfo logininfo = saveLoginInfo(dto, salt, encrypPwd);
        //保存user
        loginInfo2User(logininfo);

        return AjaxResult.me();
    }

    public User loginInfo2User(Logininfo logininfo) {
        //保存user
        User user = new User();
        BeanUtils.copyProperties(logininfo,user);
        user.setState(1);
        user.setLogininfoId(logininfo.getId());
        user.setCreateTime(new Date());
        userMapper.add(user);
        return user;
    }

    public Logininfo saveLoginInfo(UserSmsCodeDto dto, String salt, String encrypPwd) {
        //保存logininfo
        Logininfo lf = new Logininfo();
        lf.setUsername(dto.getMobile());
        lf.setSalt(salt);
        lf.setPassword(encrypPwd);
        lf.setPhone(dto.getMobile());
        lf.setDisable(1);
        lf.setType(1);
        logininfoMapper.add(lf);
        return lf;
    }
}
